package com.hxgz.steel.controller.backend;

import com.hxgz.steel.common.Const;
import com.hxgz.steel.common.ResponseCode;
import com.hxgz.steel.common.ServerResponse;
import com.hxgz.steel.domain.BackendUser;
import com.hxgz.steel.domain.User;
import com.hxgz.steel.service.BackendLoginerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("BackendLoginerController")
@Api(value = "账号管理", description = "后台:账号管理")
public class BackendLoginerController {

  @Autowired
  BackendLoginerService backendLoginerService;


  @ApiOperation("账号管理列表查询")
  @RequestMapping(value = "getbackendLoginerList.do", method = RequestMethod.POST)
  public ServerResponse<BackendUser> getbackendLoginerList(HttpServletRequest httpServletRequest) {
    User user = Const.checkBackUserLogin(httpServletRequest);
    if (user == null) {
      return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),
          ResponseCode.NEED_LOGIN.getDesc());
    }
    return backendLoginerService.getbackendLoginerList();
  }

  @ApiOperation("新增管理员或修改管理员")
  @RequestMapping(value = "addLoginer.do", method = RequestMethod.POST)
  public ServerResponse addLoginer(
      @ApiParam("传id 表示修改 不传代表添加") @RequestParam(required = false) String id,
      @ApiParam("姓名") @RequestParam(required = false) String realName,
      @ApiParam("账号") @RequestParam(required = false) String userName,
      @ApiParam("密码") @RequestParam(required = false) String password,
      HttpServletRequest httpServletRequest) {
    User user = Const.checkBackUserLogin(httpServletRequest);
    if (user == null) {
      return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),
          ResponseCode.NEED_LOGIN.getDesc());
    }
    return backendLoginerService.addLoginer(realName, userName, password, id);
  }

  @ApiOperation("删除管理员")
  @RequestMapping(value = "deleteLoginer.do", method = RequestMethod.POST)
  public ServerResponse deleteLoginer(@RequestParam String id,
      HttpServletRequest httpServletRequest) {
    User user = Const.checkBackUserLogin(httpServletRequest);
    if (user == null) {
      return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),
          ResponseCode.NEED_LOGIN.getDesc());
    }
    return backendLoginerService.deleteLoginer(id);
  }
}
